package com.uestc.qiniu;

import com.qiniu.storage.BucketManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

public class GenToken {
	private static Auth auth = Auth.create(QNConfig.ACCESS_KEY, QNConfig.SECRET_KEY);

	/**
	 * 普通上传文件
	 * 
	 * @return
	 */
	public static String getUptoken() {
		return auth.uploadToken(QNConfig.BUCKET);
	}

	/**
	 * 用key进行覆盖上传
	 * @param key
	 * @return
	 */
	public static String getUptoken(String key) {
		return auth.uploadToken(QNConfig.BUCKET, key);
	}
	
	public static String getUptoken(String callbackUrl, String callbackHost, String callbackBody) {
		StringMap sm = new StringMap();
		sm.put("callbackUrl", callbackUrl);
		sm.put("callbackBody", callbackBody);
		sm.putNotEmpty("callbackHost", callbackHost);
		return auth.uploadToken(QNConfig.BUCKET, null, 3600, sm);
	}
	
	public static String getUptoken(String key, long expires, StringMap policy) {
		return auth.uploadToken(QNConfig.BUCKET, key, expires, policy);
	}
	
	public static String getDownloadToken(String baseUrl) {
		return auth.privateDownloadUrl(baseUrl);
	}
	
	public static String getDownloadToken(String baseUrl, long expires) {
		return auth.privateDownloadUrl(baseUrl, expires);
	}
	
	public static BucketManager getBucketManager() {
		return new BucketManager(auth);
	}
	
	public static Auth getAuth() {
		return auth;
	}

	public static void main(String[] args) {
		String token = getUptoken();
		System.out.println(token);
	}

}
